# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.categoryDeptDao import CategoryDeptDao
from . import app

CORS(app, supports_credentials=True)


@app.route('/insertCategoryDept', methods=['POST'])
def insert_category_deptNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    try:
        info = {
            'categoryId': request.json['categoryId'],
            'deptIds': request.json['deptIds']
        }
    except:
        return jsonify({"message": {"code": 0, "status": "缺少必要参数"}})

    info = CategoryDeptDao().insertCatDept(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [], "message": {"code": 0, "status": "The server error"}})


@app.route('/findCategoryDeptById', methods=['POST'])
def find_category_dept_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    id = request.json.get('id', 0)
    info = CategoryDeptDao().findCatDeptById(id)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "No such data"}})


@app.route('/findCategoryDeptByCondition', methods=['POST'])
def find_category_dept_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    deptId = request.json.get('deptId', '')
    info = CategoryDeptDao().findCatDeptByConditon(deptId=deptId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteCategoryDeptByIds', methods=['POST'])
def delete_category_dept_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = CategoryDeptDao().deleteCatDept(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateCategoryDeptById', methods=['POST'])
def update_category_dept_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        catId = request.json['catId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = CategoryDeptDao().selectCatDept(catId)
    info = {
        'catId': request.json['catId'],
        'categoryId': request.json.get('categoryId', oldInfo[1]),
        'deptId': request.json.get('deptId', oldInfo[2])
    }

    info = CategoryDeptDao().updateCatDept(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findCatDeptByCatId', methods=['POST'])
def findCatDeptByCatIdNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        catId = request.json.get('catId', '')
    except:
        return jsonify({"message": {"code": 0, "status": "缺少必要参数"}})

    info = CategoryDeptDao().findCatDeptByCatId(catId=catId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})